<template>
  <ContentWrap>
    <!-- 搜索工作栏 -->
    <el-form
      class="-mb-15px"
      :model="queryParams"
      ref="queryFormRef"
      :inline="true"
      label-width="100px"
    >
      <el-form-item label="订单编号" prop="code">
        <el-input
          v-model="queryParams.code"
          placeholder="请输入订单编号"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item>
      <el-form-item label="区域" prop="agentCode">
        <el-select v-model="queryParams.agentCode" placeholder="请选择" clearable>
          <el-option
            v-for="agent in agentList"
            :key="agent.code"
            :label="agent.name"
            :value="agent.code"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="订单状态" prop="statusArr">
        <el-select v-model="queryParams.statusArr" placeholder="请选择" clearable multiple>
          <el-option
            v-for="dict in getStrDictOptions(DICT_TYPE.MEIJIA_HOME_ORDER_STATUS)"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="是否加钟" prop="overtime">
        <el-select v-model="queryParams.overtime" placeholder="请选择" clearable>
          <el-option
            v-for="dict in getStrDictOptions(DICT_TYPE.MEIJIA_OVER_TIME)"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="到家人员" prop="userCode">
        <el-select
          v-model="queryParams.userCode"
          placeholder="请选择"
          clearable
          filterable
          remote
          :remote-method="getHomeEmployeeList"
        >
          <el-option
            v-for="homeEmployee in homeEmployeeList"
            :key="homeEmployee.systemUserCode"
            :label="homeEmployee.systemUserName"
            :value="homeEmployee.systemUserCode"
          />
        </el-select>
      </el-form-item>

      <el-form-item>
        <el-button @click="handleQuery"><Icon icon="ep:search" class="mr-5px" /> 搜索</el-button>
        <el-button @click="resetQuery"><Icon icon="ep:refresh" class="mr-5px" /> 重置</el-button>
      </el-form-item>
    </el-form>
  </ContentWrap>

  <!-- 列表 -->
  <ContentWrap>
    <el-table v-loading="loading" :data="list" :stripe="true" :show-overflow-tooltip="true">
      <el-table-column label="自增" align="center" prop="id" v-if="false" />
      <el-table-column label="订单号" align="center" prop="code" />
      <el-table-column label="所属区域" align="center" prop="agentName" />
      <el-table-column label="所属渠道" align="center" prop="channelName" />
      
      <el-table-column label="订单状态" align="center" prop="status">
        <template #default="scope">
          <dict-tag :type="DICT_TYPE.MEIJIA_HOME_ORDER_STATUS" :value="scope.row.status"   />
        </template>
      </el-table-column>

      <el-table-column label="是否加钟" align="center" prop="overtime">
        <template #default="scope">
          <dict-tag :type="DICT_TYPE.MEIJIA_OVER_TIME" :value="scope.row.overtime" />
        </template>
      </el-table-column>
      <el-table-column label="客户昵称" align="center" prop="memberName" />
      <el-table-column label="到家地址" align="center" prop="addressDetail" />
      <el-table-column label="员工" align="center" prop="userName" />
      <el-table-column label="订单金额" align="center" prop="price" />
      <el-table-column label="车费" align="center" prop="fare" />
      <el-table-column label="所作项目" align="center" prop="projectNames" />
      <el-table-column
        label="创建日期"
        align="center"
        prop="createTime"
        :formatter="dateFormatter"
        width="200px"
      />
      <el-table-column label="操作" align="center">
        <template #default="scope">
          <el-button link type="primary" @click="openDrawer(scope.row.code)"> 查看详情 </el-button>
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页 -->
    <Pagination
      :total="total"
      v-model:page="queryParams.pageNo"
      v-model:limit="queryParams.pageSize"
      @pagination="getList"
    />
  </ContentWrap>
  <HomeOrderDetail ref="drawerRef" />
</template>

<script setup lang="ts">
import { dateFormatter } from '@/utils/formatTime'
import download from '@/utils/download'
import * as OrderApi from '@/api/meijia/order'
import HomeOrderDetail from './homeOrderDetail.vue'
import * as AgentApi from '@/api/meijia/agent'
import * as HomeEmployeeApi from '@/api/meijia/home/employee'
import { DICT_TYPE, getIntDictOptions, getStrDictOptions } from '@/utils/dict'
defineOptions({ name: 'OrderRefund' })

const message = useMessage() // 消息弹窗
const { t } = useI18n() // 国际化

const loading = ref(true) // 列表的加载中
const list = ref([]) // 列表的数据
const total = ref(0) // 列表的总页数
const queryParams = reactive({
  pageNo: 1,
  pageSize: 10,
  code: undefined,
  orderCode: undefined,
  status: undefined,
  remark: undefined,
  createTime: [],
  version: undefined
})
const queryFormRef = ref() // 搜索的表单
const exportLoading = ref(false) // 导出的加载中

/** 查询列表 */
const getList = async () => {
  loading.value = true
  try {
    const data = await OrderApi.getHomePageByUser(queryParams)
    list.value = data.list
    total.value = data.total
  } finally {
    loading.value = false
  }
}

/** 搜索按钮操作 */
const handleQuery = () => {
  queryParams.pageNo = 1
  getList()
}

/** 重置按钮操作 */
const resetQuery = () => {
  queryFormRef.value.resetFields()
  handleQuery()
}
const drawerRef = ref()
const openDrawer = (code: string) => {
  drawerRef.value.open(code)
}
const agentList = ref([])
const getAgentList = async () => {
  agentList.value = await AgentApi.getByAgentCode({})
}
const homeEmployeeList = ref([])
const getHomeEmployeeList = async (systemUserName?: string) => {
  if (systemUserName && systemUserName != '') {
    homeEmployeeList.value = await HomeEmployeeApi.getListBySystemUserName(systemUserName)
  } else {
    homeEmployeeList.value = []
  }
}
/** 初始化 **/
onMounted(() => {
  getHomeEmployeeList()
  getAgentList()
  getList()
})
</script>
